﻿using System;
using UnityEngine;

// Token: 0x0200012F RID: 303
[AddComponentMenu("RVP/C#/Drivetrain/Gas Motor", 0)]
[DisallowMultipleComponent]
[RequireComponent(typeof(DriveForce))]
public class GasMotor : Motor
{
	// Token: 0x06000593 RID: 1427 RVA: 0x0002B774 File Offset: 0x00029974
	public override void Start()
	{
		base.Start();
		this.targetDrive = base.GetComponent<DriveForce>();
		this.GetMaxRPM();
	}

	// Token: 0x06000594 RID: 1428 RVA: 0x0002B790 File Offset: 0x00029990
	public override void FixedUpdate()
	{
		base.FixedUpdate();
		this.actualAccel = Mathf.Lerp((!this.vp.brakeIsReverse || !this.vp.reversing || this.vp.accelInput > 0f) ? this.vp.accelInput : this.vp.brakeInput, Mathf.Max(this.vp.accelInput, this.vp.burnout), this.vp.burnout);
		float f = (!this.canReverse) ? Mathf.Clamp01(this.actualAccel) : this.actualAccel;
		this.actualInput = this.inputCurve.Evaluate(Mathf.Abs(f)) * Mathf.Sign(f);
		this.targetDrive.curve = this.torqueCurve;
		if (this.ignition)
		{
			float num = this.boostPowerCurve.Evaluate(Mathf.Abs(this.vp.localVelocity.z));
			this.targetDrive.rpm = Mathf.Lerp(this.targetDrive.rpm, this.actualInput * this.maxRPM * 1000f * ((!this.boosting) ? 1f : (1f + num)), (1f - this.inertia) * Time.timeScale);
			if (this.targetDrive.feedbackRPM > this.targetDrive.rpm)
			{
				this.targetDrive.torque = 0f;
			}
			else
			{
				this.targetDrive.torque = this.torqueCurve.Evaluate(this.targetDrive.feedbackRPM * 0.001f - ((!this.boosting) ? 0f : num)) * Mathf.Lerp(this.targetDrive.torque, this.power * (float)Mathf.Abs(Math.Sign(this.actualInput)), (1f - this.inertia) * Time.timeScale) * ((!this.boosting) ? 1f : (1f + num)) * this.health;
			}
			if (this.outputDrives.Length > 0)
			{
				float torqueFactor = Mathf.Pow(1f / (float)this.outputDrives.Length, this.driveDividePower);
				float num2 = 0f;
				foreach (DriveForce driveForce in this.outputDrives)
				{
					num2 += driveForce.feedbackRPM;
					driveForce.SetDrive(this.targetDrive, torqueFactor);
				}
				this.targetDrive.feedbackRPM = num2 / (float)this.outputDrives.Length;
			}
			if (this.transmission)
			{
				this.shifting = (this.transmission.shiftTime > 0f);
			}
			else
			{
				this.shifting = false;
			}
		}
		else
		{
			this.targetDrive.rpm = 0f;
			this.targetDrive.torque = 0f;
			this.targetDrive.feedbackRPM = 0f;
			this.shifting = false;
			if (this.outputDrives.Length > 0)
			{
				foreach (DriveForce driveForce2 in this.outputDrives)
				{
					driveForce2.SetDrive(this.targetDrive);
				}
			}
		}
	}

	// Token: 0x06000595 RID: 1429 RVA: 0x0002BB04 File Offset: 0x00029D04
	public override void Update()
	{
		if (this.snd && this.ignition)
		{
			this.airPitch = ((this.vp.groundedWheels <= 0 && this.actualAccel == 0f) ? Mathf.Lerp(this.airPitch, 0f, 0.5f * Time.deltaTime) : 1f);
			this.pitchFactor = ((this.actualAccel == 0f && this.vp.groundedWheels != 0) ? 0.5f : 1f) * ((!this.shifting) ? 1f : ((!this.pitchIncreaseBetweenShift) ? (Mathf.Min(this.transmission.shiftDelay, Mathf.Pow(this.transmission.shiftTime, 2f)) / this.transmission.shiftDelay) : Mathf.Sin(this.transmission.shiftTime / this.transmission.shiftDelay * 3.1415927f))) * this.airPitch;
			this.targetPitch = Mathf.Abs(this.targetDrive.feedbackRPM * 0.001f / this.maxRPM) * this.pitchFactor;
		}
		base.Update();
	}

	// Token: 0x06000596 RID: 1430 RVA: 0x0002BC60 File Offset: 0x00029E60
	public void GetMaxRPM()
	{
		this.maxRPM = this.torqueCurve.keys[this.torqueCurve.length - 1].time;
		if (this.outputDrives.Length > 0)
		{
			foreach (DriveForce driveForce in this.outputDrives)
			{
				driveForce.curve = this.targetDrive.curve;
				if (driveForce.GetComponent<Transmission>())
				{
					driveForce.GetComponent<Transmission>().ResetMaxRPM();
				}
			}
		}
	}

	// Token: 0x040007E0 RID: 2016
	[Tooltip("X-axis = RPM in thousands, y-axis = torque.  The rightmost key represents the maximum RPM")]
	[Header("Performance")]
	public AnimationCurve torqueCurve = AnimationCurve.EaseInOut(0f, 0f, 8f, 1f);

	// Token: 0x040007E1 RID: 2017
	[Tooltip("How quickly the engine adjusts its RPMs")]
	[Range(0f, 0.99f)]
	public float inertia;

	// Token: 0x040007E2 RID: 2018
	[Tooltip("Can the engine turn backwards?")]
	public bool canReverse;

	// Token: 0x040007E3 RID: 2019
	private DriveForce targetDrive;

	// Token: 0x040007E4 RID: 2020
	[NonSerialized]
	public float maxRPM;

	// Token: 0x040007E5 RID: 2021
	public DriveForce[] outputDrives;

	// Token: 0x040007E6 RID: 2022
	[Tooltip("Exponent for torque output on each wheel")]
	public float driveDividePower = 3f;

	// Token: 0x040007E7 RID: 2023
	private float actualAccel;

	// Token: 0x040007E8 RID: 2024
	[Header("Transmission")]
	public GearboxTransmission transmission;

	// Token: 0x040007E9 RID: 2025
	[NonSerialized]
	public bool shifting;

	// Token: 0x040007EA RID: 2026
	[Tooltip("Increase sound pitch between shifts")]
	public bool pitchIncreaseBetweenShift;
}
